package reduce.fz.com.reduce.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.pnikosis.materialishprogress.ProgressWheel;

import reduce.fz.com.reduce.R;

/**
 * Created by qingshengzheng on 15/7/5.
 */
public class NetImage extends FrameLayout {
    private Context context;
    private AttributeSet attrs;
    private NetworkImageView mBannerItemImage;
    private ProgressWheel mImageProgressWheel;
    /*图片缓存*/
    private ImageLoader imageLoader;


    public NetImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        LayoutInflater.from(context).inflate(R.layout.layout_view_netimage, this, true);
        this.initWidget();

    }

    public void initWidget() {
        mBannerItemImage = (NetworkImageView) this.findViewById(R.id.banner_item_image);
        mImageProgressWheel = (ProgressWheel) this.findViewById(R.id.image_progress_wheel);
    }

    public void initAttr() {
        mBannerItemImage.setErrorImageResId(R.drawable.image_error_background);
        mBannerItemImage.setDefaultImageResId(R.drawable.image_error_background_gray);
        mBannerItemImage.setOnImageCompleteListener(new NetworkImageView.OnImageCompleteListener() {
            @Override
            public void onSuccess(ImageLoader.ImageContainer response) {
                if (response != null && response.getBitmap() != null) {
                    mImageProgressWheel.setVisibility(View.GONE);
                }
            }

            @Override
            public void onSuccessFromCache() {
                mImageProgressWheel.setVisibility(View.GONE);
            }

            @Override
            public void onError(VolleyError error) {
                mImageProgressWheel.setVisibility(View.GONE);
            }
        });
    }

    public void setImageUrl(String url, ImageLoader imageLoader) {
        if (imageLoader == null)
            throw new RuntimeException();
        mBannerItemImage.setImageUrl(url, imageLoader);
    }


}
